/******************************************************************************
 *
 * MotionViewer
 * Copyright (C) 2007 Sangyoon Lee, Electronic Visualization Laboratory, 
 * University of Illinois at Chicago
 *
 * This software is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either Version 2.1 of the License, or 
 * (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
 * License for more details.
 * 
 * You should have received a copy of the GNU Lesser Public License along
 * with this software; if not, write to the Free Software Foundation, Inc., 
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * Questions or comments about MotionViewer should be directed to 
 * cavern@evl.uic.edu
 *
 *****************************************************************************/
#include <ogreplatform.h>
#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
#include <Carbon/Carbon.h>
#endif

#include "MocapVisualizer.h"
#include "ViconRTVisualizer.h"
#include "ViconVVisualizer.h"
#include "ViconRTData.h"
#include "ViconVFileData.h"



MocapVisualizer::MocapVisualizer(SceneManager* mgr, SceneNode* head)
{
	//m_pSceneMgr = mgr;
	//m_pBaseNode = head;

	//initVisualizer();
}

MocapVisualizer::~MocapVisualizer()
{
}

void MocapVisualizer::initVisualizer()
{
	// generate custom material
	m_pGridMaterial = MaterialManager::getSingleton().create("mocapGridMaterial","mocapVisualizer"); 
	m_pGridMaterial->setReceiveShadows(false); 
	m_pGridMaterial->getTechnique(0)->setLightingEnabled(true); 
	m_pGridMaterial->getTechnique(0)->getPass(0)->setDiffuse(0.5,0.5,0.5,0); 
	m_pGridMaterial->getTechnique(0)->getPass(0)->setAmbient(0.5,0.5,0.5); 
	m_pGridMaterial->getTechnique(0)->getPass(0)->setSelfIllumination(0.5,0.5,0.5); 
	// red mat
	m_pRedMaterial = MaterialManager::getSingleton().create("mocapRedMaterial","mocapVisualizer"); 
	m_pRedMaterial->setReceiveShadows(false); 
	m_pRedMaterial->getTechnique(0)->setLightingEnabled(true); 
	m_pRedMaterial->getTechnique(0)->getPass(0)->setDiffuse(0.9,0.0,0.0,0); 
	m_pRedMaterial->getTechnique(0)->getPass(0)->setAmbient(0.9,0.0,0.0); 
	m_pRedMaterial->getTechnique(0)->getPass(0)->setSelfIllumination(0.9,0.0,0.0);
	// gree mat
	m_pGreenMaterial = MaterialManager::getSingleton().create("mocapGreenMaterial","mocapVisualizer"); 
	m_pGreenMaterial->setReceiveShadows(false); 
	m_pGreenMaterial->getTechnique(0)->setLightingEnabled(true); 
	m_pGreenMaterial->getTechnique(0)->getPass(0)->setDiffuse(0.0,0.9,0.0,0); 
	m_pGreenMaterial->getTechnique(0)->getPass(0)->setAmbient(0.0,0.9,0.0); 
	m_pGreenMaterial->getTechnique(0)->getPass(0)->setSelfIllumination(0.0,0.9,0.0);
	// blue mat
	m_pBlueMaterial = MaterialManager::getSingleton().create("mocapBlueMaterial","mocapVisualizer"); 
	m_pBlueMaterial->setReceiveShadows(false); 
	m_pBlueMaterial->getTechnique(0)->setLightingEnabled(true); 
	m_pBlueMaterial->getTechnique(0)->getPass(0)->setDiffuse(0.0,0.0,0.9,0); 
	m_pBlueMaterial->getTechnique(0)->getPass(0)->setAmbient(0.0,0.0,0.9); 
	m_pBlueMaterial->getTechnique(0)->getPass(0)->setSelfIllumination(0.0,0.0,0.9);

	// generate grid
	m_pGridObject =  m_pSceneMgr->createManualObject("manualGrid"); 
	SceneNode* myGridObjectNode = m_pBaseNode->createChildSceneNode("manual_grid_node"); 
	m_pGridObject->begin("mocapGridMaterial", Ogre::RenderOperation::OT_LINE_LIST);
	float gx = 1000;
	float gz = 1000;
	float x,z, dx, dz;
	dx = gx / 10;
	dz = gz / 10;
	int i;
	for (i=0; i<11; i++)
	{
		x = -gx/2.0f + dx*i;
		m_pGridObject->position(x, 0, -gz/2); 
		m_pGridObject->position(x, 0, gz/2);
		
		z = -gz/2.0f + dz*i;
		m_pGridObject->position(-gx/2, 0, z); 
		m_pGridObject->position(gx/2, 0, z);
	}
	m_pGridObject->end(); 
	myGridObjectNode->attachObject(m_pGridObject);

	// generate world coordinate arrows (30cm arrows with 5cm cube on origin)
	MeshPtr mesh = MeshManager::getSingleton().load("10cm_Y_arrow.mesh", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
	Entity* pArrow = m_pSceneMgr->createEntity("y_arrow", "10cm_Y_arrow.mesh");
	pArrow->setMaterialName("mocapGreenMaterial");
	SceneNode* pNode = m_pBaseNode->createChildSceneNode("y_arrow");
	pNode->scale(Ogre::Vector3(2, 3, 2));
	pNode->attachObject(pArrow);
	
	pArrow = m_pSceneMgr->createEntity("x_arrow", "10cm_Y_arrow.mesh");
	pArrow->setMaterialName("mocapRedMaterial");
	pNode = m_pBaseNode->createChildSceneNode("x_arrow");
	pNode->scale(Ogre::Vector3(2, 3, 2));
	pNode->rotate(Ogre::Vector3(0, 0, 1), Degree(-90));
	pNode->attachObject(pArrow);
	
	pArrow = m_pSceneMgr->createEntity("z_arrow", "10cm_Y_arrow.mesh");
	pArrow->setMaterialName("mocapBlueMaterial");
	pNode = m_pBaseNode->createChildSceneNode("z_arrow");
	pNode->scale(Ogre::Vector3(2, 3, 2));
	pNode->rotate(Ogre::Vector3(1, 0, 0), Degree(90));
	pNode->attachObject(pArrow);
	
	mesh = MeshManager::getSingleton().load("unit_box.mesh", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
	pArrow = m_pSceneMgr->createEntity("origin_cube", "unit_box.mesh");
	pArrow->setMaterialName("mocapGridMaterial");
	pNode = m_pBaseNode->createChildSceneNode("origin_cube");
	pNode->scale(Ogre::Vector3(5, 5, 5));
	pNode->attachObject(pArrow);

}

void MocapVisualizer::update(float addedTime)
{

}
